﻿using System;
using System.Collections;
using System.Text;
using System.Data;
using GenaralLibs;
using System.Net.Mail;
namespace Lists
{
    public class HoSoCMND : DoiTuong
    {
        #region string HoTen property
        public string HoTen
        {
            get
            {
                if (_DataRow["HoTen"] is string) return (string)_DataRow["HoTen"];
                else return "";
            }
            set
            {
                _DataRow["HoTen"] = value;
            }
        }
        #endregion

        #region byte[] AnhDaiDien property
        public byte[] AnhDaiDien
        {
            get
            {
                if (_DataRow["AnhDaiDien"] is byte[]) return (byte[])_DataRow["AnhDaiDien"];
                return new byte[0];
                //return "";
            }
            set
            {
                _DataRow["AnhDaiDien"] = (byte[])value;
            }
        }
        #endregion

        #region bool GioiTinh property
        public bool? GioiTinh
        {
            get
            {
                if (_DataRow["GioiTinh"] is bool) return (bool)_DataRow["GioiTinh"];
                return null;
            }
            set
            {
                _DataRow["GioiTinh"] = value;
            }
        }
        #endregion

        #region DateTime NgaySinh property
        public DateTime? NgaySinh
        {
            get
            {
                if (_DataRow["NgaySinh"] is DateTime) return (DateTime)_DataRow["NgaySinh"];
                return null;
            }
            set
            {
                _DataRow["NgaySinh"] = value;
            }
        }
        #endregion

        #region string SoCMND property
        public string SoCMND
        {
            get
            {
                if (_DataRow["SoCMND"] is string) return (string)_DataRow["SoCMND"];
                return "";
            }
            set
            {
                _DataRow["SoCMND"] = value;
            }
        }
        #endregion

        #region DateTime NgayCap_CMND property
        public DateTime? NgayCap_CMND
        {
            get
            {
                if (_DataRow["NgayCap_CMND"] is DateTime) return (DateTime)_DataRow["NgayCap_CMND"];
                return null;
            }
            set
            {
                _DataRow["NgayCap_CMND"] = value;
            }
        }
        #endregion

        #region string DonViCap property
        public string DonViCap
        {
            get
            {
                if (_DataRow["DonViCap"] is string) return (string)_DataRow["DonViCap"];
                return "";
            }
            set
            {
                _DataRow["DonViCap"] = value;
            }
        }
        #endregion

        #region int DanTocID property
        public int DanTocID
        {
            get
            {
                if (_DataRow["DanTocID"] is int) return (int)_DataRow["DanTocID"];
                return 0;
            }
            set
            {
                _DataRow["DanTocID"] = value;
            }
        }
        #endregion

        #region int QuocTichID property
        public int QuocTichID
        {
            get
            {
                if (_DataRow["QuocTichID"] is int) return (int)_DataRow["QuocTichID"];
                return 0;
            }
            set
            {
                _DataRow["QuocTichID"] = value;
            }
        }
        #endregion

        #region string HK_ThuongTru property
        public string HK_ThuongTru
        {
            get
            {
                if (_DataRow["HK_ThuongTru"] is string) return (string)_DataRow["HK_ThuongTru"];
                return "";
            }
            set
            {
                _DataRow["HK_ThuongTru"] = value;
            }
        }
        #endregion

        #region string DiaChi property
        public string DiaChi
        {
            get
            {
                if (_DataRow["DiaChi"] is string) return (string)_DataRow["DiaChi"];
                return "";
            }
            set
            {
                _DataRow["DiaChi"] = value;
            }
        }
        #endregion

        public HoSoCMND(DataRow dataRow)
            : base(dataRow)
        {
        }
    }

    public class DanhMucHoSoCMND : DanhMucDoiTuong, IEnumerable
    {
        #region Instance property: Singleton Design pattern
        private static DanhMucHoSoCMND _Instance;
        public static DanhMucHoSoCMND Instance
        {
            get
            {
                if (_Instance == null)
                {
                    _Instance = new DanhMucHoSoCMND();
                    _Instance.DataView = _Instance.DataTable.DefaultView;
                }
                return _Instance;
            }
        }
        #endregion

        #region override TableName property
        public override string TableName
        {
            get
            {
                return "HoSoCMND";
            }
        }
        #endregion

        #region Methods required by IEnumerable (use for foreach)
        public IEnumerator GetEnumerator()
        {
            return new TokenEnumerator(this);
        }

        private class TokenEnumerator : IEnumerator
        {
            private int _Position = -1;
            DanhMucDoiTuong _Dstk;

            public TokenEnumerator(DanhMucDoiTuong dstk)
            {
                _Dstk = dstk;
            }

            // Declare the MoveNext method required by IEnumerator:
            public bool MoveNext()
            {
                if (_Position < _Dstk.DataView.Count - 1)
                {
                    _Position++;
                    return true;
                }
                else
                {
                    return false;
                }
            }

            // Declare the Reset method required by IEnumerator:
            public void Reset()
            {
                _Position = -1;
            }

            // Declare the Current property required by IEnumerator:
            public object Current
            {
                get
                {
                    return new HoSoCMND((_Dstk.DataView[_Position].Row));
                }
            }
        }
        #endregion

        public HoSoCMND this[int index]
        {
            get
            {
                if (0 <= index && index <= this.Count)
                {
                    return new HoSoCMND(DataTable.Rows[index]);
                }
                else
                {
                    return null;
                }
            }
        }

        public HoSoCMND NewDoiTuong()
        {
            try
            {
                return new HoSoCMND(Instance.DataTable.NewRow());
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public HoSoCMND GetByProperty(string property, object value)
        {
            try
            {
                DataRow[] rows = GetDataRows(property, value);
                if (rows.Length > 0) return new HoSoCMND(rows[0]);
                else return null;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public HoSoCMND GetByID(int objID)
        {
            try
            {
                return GetByProperty("ID", objID);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public HoSoCMND GetBySoCMND(string soCMND)
        {
            try
            {
                return GetByProperty("SoCMND", soCMND);
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucHoSoCMND Filter(string filterExpression)
        {
            try
            {
                DanhMucHoSoCMND obj = new DanhMucHoSoCMND();
                obj.DataView = new DataView(DanhMucHoSoCMND.Instance.DataTable, filterExpression, "", DataViewRowState.CurrentRows);
                return obj;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }

        public static DanhMucHoSoCMND GetAll()
        {
            try
            {
                return Instance;
            }
            catch (Exception ex)
            {
                if (ex is CustomException) throw ex;
                else
                {
                    throw new Exception(ex.Message);
                }
            }
        }
    }
}